﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
using System.IO;

namespace SNPAD
{
    public partial class FrmCodeSoft : Form
    {
        public FrmCodeSoft()
        {
            InitializeComponent();
        }
        public int ilableLx;//打印模板类型，目前这里仅一个模板  
        public DataGridView dv;//定义公共变量，调用这个窗口时把datagridview传进来，  
        

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                Close();
            }
            else if (keyData == Keys.Enter)
            {

                SendKeys.Send("{tab}");
                return true;

            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
        private void FrmCodeSoft_Load(object sender, EventArgs e)
        {

        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (ilableLx == 0)//模板一  
            {
                LabelManager2.Application labApp = new LabelManager2.Application();
                LabelManager2.Document doc = null;
                string labFileName = System.Windows.Forms.Application.StartupPath + @"\barJapLab.Lab";
                try
                {

                    if (!File.Exists(labFileName))
                    {
                        MessageBox.Show("沒有找到標簽模板文件：barJapLab.Lab,請聯系系統管理員", "溫馨提示");
                        return;
                    } 
                    labApp.Documents.Open(labFileName, false);// 调用设计好的label文件  
                    doc = labApp.ActiveDocument;
                    if (dv.SelectedRows.Count > 0)
                    {
                        progressBar1.Maximum = dv.SelectedRows.Count;
                        progressBar1.Value = 0;

                        for (int i = 0; i < dv.SelectedRows.Count; i++)  //选中多行打印  
                        {
                            //下面这些是给模板的变量传值进去                           
                            doc.Variables.FormVariables.Item("barcode_no").Value = dv.SelectedRows[i].Cells["barcode_no"].Value.ToString();
                            doc.Variables.FormVariables.Item("style_no").Value = dv.SelectedRows[i].Cells["style_no"].Value.ToString();
                            doc.Variables.FormVariables.Item("upp_jp").Value = dv.SelectedRows[i].Cells["upp_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("outsole_jp").Value = dv.SelectedRows[i].Cells["outsole_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("col_jp").Value = dv.SelectedRows[i].Cells["col_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("size_width").Value = dv.SelectedRows[i].Cells["size_width"].Value.ToString();
                            doc.Variables.FormVariables.Item("price").Value = dv.SelectedRows[i].Cells["price"].Value.ToString();
                            doc.Variables.FormVariables.Item("comp_jp").Value = dv.SelectedRows[i].Cells["comp_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("address_jp").Value = dv.SelectedRows[i].Cells["address_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("url").Value = dv.SelectedRows[i].Cells["url"].Value.ToString();
                            //下面这行是打印份数的定义  
                            doc.PrintDocument(Convert.ToInt16(dv.SelectedRows[i].Cells["qty"].Value) + Convert.ToInt16(dv.SelectedRows[i].Cells["att_qty"].Value));
                            progressBar1.Value++;
                            progressBar1.Refresh();
                            lbStatus.Text = "總共" + dv.SelectedRows.Count.ToString() + "行需要列印,已送出" + Convert.ToString(i + 1) + "行";
                            lbStatus.Refresh();
                            this.Refresh();

                        }
                    }
                    else  //光标所在行打印  
                    {
                        doc.Variables.FormVariables.Item("barcode_no").Value = dv.CurrentRow.Cells["barcode_no"].Value.ToString();
                        doc.Variables.FormVariables.Item("style_no").Value = dv.CurrentRow.Cells["style_no"].Value.ToString();
                        doc.Variables.FormVariables.Item("upp_jp").Value = dv.CurrentRow.Cells["upp_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("outsole_jp").Value = dv.CurrentRow.Cells["outsole_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("col_jp").Value = dv.CurrentRow.Cells["col_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("size_width").Value = dv.CurrentRow.Cells["size_width"].Value.ToString();
                        doc.Variables.FormVariables.Item("price").Value = dv.CurrentRow.Cells["price"].Value.ToString();
                        doc.Variables.FormVariables.Item("comp_jp").Value = dv.CurrentRow.Cells["comp_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("address_jp").Value = dv.CurrentRow.Cells["address_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("url").Value = dv.CurrentRow.Cells["url"].Value.ToString();
                        doc.PrintDocument(Convert.ToInt16(dv.CurrentRow.Cells["qty"].Value));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("出錯啦，原因如下：\n\r" + ex.Message, "出錯啦");
                }
                finally
                {
                    labApp.Documents.CloseAll(true);
                    labApp.Quit();//退出  
                    labApp = null;
                    doc = null;
                    GC.Collect(0);
                }


            }
        }
    }
}
